package com.ming_za.odupdaterserver.core.vo;

import com.ming_za.odupdaterserver.core.exception.ROEnum;
import com.ming_za.odupdaterserver.core.utils.obj.ReturnPackage;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

@Schema(description = "统一返回")
@Data
public class R<T>{

    @Schema(description = "状态码")
    private int code;
    @Schema(description = "提示信息")
    private String message;
    @Schema(description = "数据")
    private T data;

    public R(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static<V> R<V> ok(){
        return ok(null);
    }

    public static<V> R<V> ok(V data){
        return new R<>(200, "ok", data);
    }

    public static<V> R<V> error(){
        return error(null);
    }

    public static<V> R<V> error(V data){
        return error(500,"意外错误",data);
    }

    public static<V> R<V> error(int code,String msg){
        return error(code,msg,null);
    }

    public static<V> R<V> error(int code,String msg,V data){
        return new R<>(code,msg,data);
    }

    public static<V> R<V> pack(ROEnum r){
        return pack(r,null);
    }

    public static<V> R<V> pack(ROEnum r,V data){
        return new R<>(r.getCode(),r.getDefaultMsg(),data);
    }

    public static<V> R<V> pack(ROEnum r,String msg){
        return new R<>(r.getCode(),msg,null);
    }
}
